package com.egoo.ticket.server.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.pojo.dto.EmailMsgDto;
import com.egoo.ticket.server.pojo.dto.SystemMsgDto;
import com.egoo.ticket.server.service.EripDataSynService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author：victor_tang
 * @Date ：2020/2/27
 * @Description：erip对接Controller
 */
@Slf4j
@RestController
@RequestMapping(value = "/v1/EripDataSyn", produces = "application/json")
@Api(value = "Erip数据对接管理",tags = "Erip数据对接管理",description = "Erip数据对接管理")
public class EripDataSynController {

	@Autowired
	private EripDataSynService eripDataSynService;

	/**
	 * TODO 提供给ERIP，触发同步用户，角色，部门数据
	 * @author: victor_tang
	 * @createtime: 2020/4/2 14:24
	 * @param jsonObject 1
	 * @return: com.egoo.ews.service.utils.DBResponse
	 */
	@ApiOperation(value = "提供给ERIP，触发同步用户，角色，部门数据", notes = "提供给ERIP，触发同步用户，角色，部门数据")
	@RequestMapping(method = RequestMethod.POST, value = "/workOrderSynEurmData")
	public DbResponse workOrderSynEurmData(@RequestBody JSONObject jsonObject) {

//		log.info("ERIP传来的同步数据为："+jsonObject.toJSONString());
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,"此接口已作废");
//		if (jsonObject==null) {
//			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "ERIP传来的同步数据为空");
//		}
//		String dataType = jsonObject.getJSONObject("info").getString("dataType");
//		if (ConstantCode.SynEurmDataType.USER.equals(dataType)) {
//			return eripDataSynService.userDataSyn(null);
//		}
//		else if (ConstantCode.SynEurmDataType.ROLE.equals(dataType)) {
//			return eripDataSynService.roleDataSyn(null);
//		}
//		else if (ConstantCode.SynEurmDataType.DEPT.equals(dataType)) {
//			return eripDataSynService.deptDataSyn(null);
//		}
//		else{
//			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
//					"此dataType不是工单干系统同步的数据类型，请确认！");
//		}
	}
	/**
	 * TODO 用户数据同步
	 * 
	 * @param 
	 * @return
	 */
	@ApiOperation(value = "用户数据同步", notes = "用户数据同步")
	@RequestMapping(method = RequestMethod.POST, value = "/userDataSyn")
	public DbResponse userDataSyn(@RequestBody String user) {
		return eripDataSynService.userDataSyn(user);
	}

	/**
	 * TODO 角色数据同步
	 *
	 * @param role
	 * @return
	 */
	@ApiOperation(value = "角色数据同步", notes = "角色数据同步")
	@RequestMapping(method = RequestMethod.POST, value = "/roleDataSyn")
	public DbResponse roleDataSyn(@RequestBody String role) {
		return eripDataSynService.roleDataSyn(role);
	}
	/**
	 * TODO 部门数据同步
	 * @author: victor_tang
	 * @createtime: 2020/3/17 12:17
	 * @param dept 1
	 * @return: com.egoo.ews.service.utils.DBResponse
	 */
	@ApiOperation(value = "部门数据同步", notes = "部门数据同步")
	@RequestMapping(method = RequestMethod.POST, value = "/deptDataSyn")
	public DbResponse deptDataSyn(@RequestBody String dept) {
		return eripDataSynService.deptDataSyn(dept);
	}
	@ApiOperation(value = "获取用户权限列表", notes = "获取用户权限列表")
	@RequestMapping(method = RequestMethod.GET, value = "/getUserPermissionsList")
	public DbResponse getUserPermissionsList(
            @ApiParam(value = "userId", required = true) @RequestParam("userId") String userId,
            @ApiParam(value = "tenantId", required = true) @RequestParam(
					"tenantId") String tenantId, HttpServletRequest request) {
		try {
			userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
			return eripDataSynService.getUserPermissionsList(tenantId, userId);
		}catch (Exception e){
			log.error("获取用户权限列表异常："+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,
					"获取用户权限列表异常："+e);
		}
	}
//	/**
//	 * TODO 获取产品型号信息列表
//	 * @author: victor_tang
//	 * @createtime: 2020/3/2 18:03
//	 * @param productModelInfoDto 1
//	 * @return: com.egoo.ews.service.utils.DBResponse
//	 */
//	@ApiOperation(value = "获取产品型号信息列表", notes = "获取产品型号信息列表")
//	@RequestMapping(method = RequestMethod.POST, value = "/getProductModelList")
//	public DbResponse getProductModelList(@RequestBody ProductModelInfoDto productModelInfoDto) {
//
//		return eripDataSynService.getProductModelList(productModelInfoDto);
//	}
	/**
	 * TODO 获取业务分类信息列表
	 * @author: victor_tang
	 * @createtime: 2020/3/17 12:55
	 * @param
	 * @return: com.egoo.ews.service.utils.DBResponse
	 */
	@ApiOperation(value = "获取业务分类信息列表", notes = "获取业务分类信息列表")
	@RequestMapping(method = RequestMethod.GET, value = "/getBusinesTypeList")
	public DbResponse getBusinesTypeList() {

		return eripDataSynService.getBusinesTypeList();
	}
//	/**
//	 * TODO 角色数据同步
//	 *
//	 * @param
//	 * @return
//	 */
//	@ApiOperation(value = "历史工单当前处理人数据同步", notes = "历史工单当前处理人数据同步")
//	@RequestMapping(method = RequestMethod.GET, value = "/hisWorkOrderCurrentHandlePersonSyn")
//	public DbResponse hisWorkOrderCurrentHandlePersonSyn() {
//
//		try {
//			return eripDataSynService.hisWorkOrderCurrentHandlePersonSyn();
//		} catch (Exception e) {
//			log.error("历史工单当前处理人数据同步异常：" + e);
//			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, "历史工单当前处理人数据同步异常：" + e);
//		}
//	}
	/**
	 * TODO 发送短信
	 *
	 * @param msgDto
	 * @return
	 */
	@ApiOperation(value = "发送短信", notes = "发送短信")
	@RequestMapping(method = RequestMethod.POST, value = "/sendMessage")
	public DbResponse sendMessage(@RequestBody EmailMsgDto msgDto,
								  HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		if (StringUtils.isEmpty(msgDto.getPhone())) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "手机号不能为空！");
		}
		if (StringUtils.isEmpty(msgDto.getMsgContent())) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "消息内容不能为空！");
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("phoneNumber", msgDto.getPhone());
		jsonObject.put("content", msgDto.getMsgContent());
		jsonObject.put("workOrderId",msgDto.getWorkOrderId());
		jsonObject.put("userId",userId);
		jsonObject.put("msgTypeName",msgDto.getMsgTypeName());
		jsonObject.put("clientPhone",msgDto.getPhone());
		jsonObject.put("tenantId",tenantId);
		return  eripDataSynService.sendMsg(jsonObject);
	}

	/**
	 * TODO 获取短信模板
	 *
	 * @param classify
	 * @return
	 */
	@ApiOperation(value = "获取短信模板", notes = "获取短信模板")
	@RequestMapping(method = RequestMethod.GET, value = "/getMsgTemplate")
	public DbResponse getMsgTemplate(@RequestParam(name = "classify",required = false) String classify) {
		try {
			JSONArray msgTemplate = eripDataSynService.getTemplate();
			if (msgTemplate==null || msgTemplate.size()==0) {
				return new DbResponse(ResponseConstant.RES_FAIL_CODE, "获取短信模板为空！");
			}
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, "获取短信模板成功！",msgTemplate);
		} catch (Exception e) {
			log.info("获取短信模板异常:"+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "获取短信模板异常:"+e);
		}
	}
	/**
	 * TODO 发送系统消息
	 * @author: victor_tang
	 * @createtime: 2020/3/22 16:23
	 * @param systemMsgDto 1
	 * @return: com.egoo.ews.service.utils.DBResponse
	 */
	@ApiOperation(value = "发送系统消息", notes = "发送系统消息")
	@RequestMapping(method = RequestMethod.POST, value = "/senSystemMsg")
	public DbResponse senSystemMsg(@RequestBody SystemMsgDto<List> systemMsgDto) {
		if (StringUtils.isEmpty(systemMsgDto.getContent())) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "发送内容不能为空！");
		}
		if (systemMsgDto.getTargets()==null||systemMsgDto.getTargets().size()==0) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "接收人不能为空！");
		}
		if (StringUtils.isEmpty(systemMsgDto.getSenderId())) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "发送人id不能为空!");
		}
		return eripDataSynService.sendSystemMsg(systemMsgDto);
	}

	@ApiOperation(value = "发送邮件", notes = "发送邮件")
	@RequestMapping(method = RequestMethod.POST, value = "/sendEmail")
	public DbResponse sendEmail(@RequestBody SystemMsgDto<List> systemMsgDto,HttpServletRequest request) {
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);

		if (StringUtils.isEmpty(systemMsgDto.getTitle())){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"邮件标题不能为空");
		}
		if (StringUtils.isEmpty(systemMsgDto.getContent())){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"邮件内容不能为空");
		}
		if (CollectionUtils.isEmpty(systemMsgDto.getTargets())){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"接收人邮箱不能为空");
		}
//		if (StringUtils.isEmpty(systemMsgDto.getSenderId())){
//			systemMsgDto.setSenderId(userId);
//		}

		systemMsgDto.setTenantId(tenantId);
		return eripDataSynService.sendRevisitEmail(systemMsgDto);
	}

	@ApiOperation(value = "获取工作台主题色", notes = "获取工作台主题色")
	@RequestMapping(method = RequestMethod.GET, value = "/getMajorColor")
	public DbResponse getMajorColor(HttpServletRequest request){
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		try {
			DbResponse majorColor = eripDataSynService.getMajorColor(tenantId);
			return majorColor;
		} catch (Exception e) {
			log.error("获取主题色异常"+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,ResponseConstant.SELECT_FAIL_MSG);
		}
	}
	@ApiOperation(value = "浙能推送工单信息至ERIP系统CRM系统adapter", notes = "浙能推送工单信息至ERIP系统CRM系统adapter")
	@RequestMapping(method = RequestMethod.POST, value = "/pushWorkOrderInfoToCrmAdapter")
	public DbResponse pushWorkOrderInfoToCrmAdapter(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
		JSONObject result = eripDataSynService.pushWorkOrderInfo(jsonObject);
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, "请求成功!",result);
	}

}
